package com.qjc.demo.train.half;

/**
 * @ClassName: HalfDemo2
 * @Description: 用递归的方式实现二分查找法
 * @Author: qjc
 * @Date: 2022/5/1 1:26 PM
 */
public class HalfDemo2 {

    public static int bsearchInternally(int[] nums, int low, int high, int t) {
        if (low > high) return -1;
        int mid = (low + high) / 2;
        if (t == nums[mid]) {
            return mid;
        }
        if (t < nums[mid]) {
            return bsearchInternally(nums, low, mid - 1, t);
        } else {
            return bsearchInternally(nums, mid + 1, high, t);
        }
    }

    public static int bsearch(int[] nums, int t) {
        return bsearchInternally(nums, 0, nums.length - 1, t);
    }

    public static void main(String[] args) {
        int[] nums = {3, 12, 24, 31, 46, 48, 52, 66, 69, 79, 82};
        int i = bsearch(nums, 46);
        System.err.println(i);
    }

}
